//
//  MainViewController.m
//  JackDeve
//
//  Created by apple on 17/5/9.
//  Copyright © 2017年 wkq. All rights reserved.


#import "MainViewController.h"
#import "HeaderFile.h"

@interface MainViewController ()<MAMapViewDelegate>


{
    MAMapView *_theMapView;
    
    
    
    const int a;
    const int *s;
    int *const b;
    int const *c;
    
}
    
    
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jsonData];

    _theMapView = [[MAMapView alloc]initWithFrame:self.view.frame];
   
    //显示用户位置
//    _theMapView.mapType =MAMapTypeStandard;
    
    MAUserLocationRepresentation *userLocation = [[MAUserLocationRepresentation alloc]init];
    //定位用户位置的模式    
    [_theMapView updateUserLocationRepresentation:userLocation];
    
    
    _theMapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    
    //显示比例尺
    _theMapView.showsScale = YES;
    _theMapView.scaleOrigin = CGPointMake(_theMapView.scaleOrigin.x, SCREEN_HEIGHT-50);
    
    //显示指南针
    _theMapView.showsCompass = YES;
    _theMapView.compassOrigin = CGPointMake(_theMapView.compassOrigin.x,114);
    
    _theMapView.delegate = self;

    [self.view addSubview:_theMapView];
    
    
}

-(void)jsonData{
    NSString *mainUrl = [NSString stringWithFormat:@"%@/index/get_diners?lng=116.490667&lat=39.906133&diner_type=0&is_delivery=0",ROOT_URL];
//    NSLog(@"mainUrl = %@",mainUrl);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:mainUrl]] completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        MainModel *model = [MainModel yy_modelWithJSON:dic];
        for (NSDictionary *ddd in model.data) {
            jsonData *da = [jsonData yy_modelWithDictionary:ddd];
            NSLog(@"%@",da);

            [self locateToLatitude:[da.lat_now floatValue] longitude:[da.lng_now floatValue]];
        }
    }];
    [task resume];
    
    
    
}

- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
    
    CLLocationCoordinate2D center = {latitude , longitude};
    
    MACoordinateSpan span;
    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;
    
    MACoordinateRegion region = {center,span};
    [_theMapView setRegion:region animated:YES];
    
    MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
//    annotation.title = @"早餐车";
    CLLocationCoordinate2D coordinate = {latitude , longitude};
    annotation.coordinate = coordinate;
    [_theMapView addAnnotation:annotation];
}

#pragma mark 地图中锚点
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    static NSString* annoId = @"fkAnno";
    // 获取可重用的锚点控件
    
    MAAnnotationView* annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoId];
    // 如果可重用的锚点控件不存在，创建新的可重用锚点控件
    if (!annoView){
        annoView= [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];
    }

    if (annoView.annotation.coordinate.latitude == 39.156834) {
        // 为锚点控件设置图片
        annoView.image = [UIImage imageNamed:@"选中定位"];
        // 设置该锚点控件是否可显示气泡信息
        annoView.canShowCallout = YES;
        annoView.annotation.title = @"餐车";
        // 定义一个按钮，用于为锚点控件设置附加控件
        annoView.enabled = YES;
        annoView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moringCar:)];
        [annoView addGestureRecognizer:tap];
        [self mapView:_theMapView didSelectAnnotationView:annoView];
        
        return annoView;
    }else if (annoView.annotation == _theMapView.userLocation){
        
        return annoView;
    }
    else {
        // 为锚点控件设置图片
        annoView.image = [UIImage imageNamed:@"早餐店 copy 4"];
        annoView.annotation.title = @"餐店";
        // 设置该锚点控件是否可显示气泡信息
        annoView.canShowCallout = YES;
        // 定义一个按钮，用于为锚点控件设置附加控件
        annoView.enabled = YES;
        
        annoView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moringShop:)];
        [annoView addGestureRecognizer:tap];
        [self mapView:_theMapView didSelectAnnotationView:annoView];


        return annoView;
    }
  
}

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    MAUserLocation *Location = _theMapView.userLocation;
//    NSLog(@"%@",Location);
    CLHeading *heading = Location.heading;
    
}
// 选中大头针
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    NSLog(@"选中大头针");
}

- (void)moringCar:(id)sender
{
    NSLog(@"您点击了锚点信息！");
}
- (void)moringShop:(id)sender
{
    NSLog(@"您点击了锚点信息！");
}

@end
